package cn.com.css.taxInspection.zdgz.service.impl;

import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.zdgz.entity.GzcbEntity;
import cn.com.css.taxInspection.zdgz.mapper.GzcbMapper;
import cn.com.css.taxInspection.zdgz.service.GzcbService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service("GzcbService")
public class GzcbServiceImpl extends ServiceImpl<GzcbMapper, GzcbEntity> implements GzcbService {

    @Resource
    private HttpSessionService httpSessionService;

    @Override
    public IPage<GzcbEntity> queryGzcbData(GzcbEntity gzcbEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String substring = currentDeptId.substring(0, 9);
        IPage<GzcbEntity> page = new Page<>(gzcbEntity.getPage(), gzcbEntity.getLimit());
        LambdaQueryWrapper<GzcbEntity> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(gzcbEntity.getAjbh())) {
            queryWrapper.like(GzcbEntity::getAjbh, gzcbEntity.getAjbh());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getNsrsbh())) {
            queryWrapper.like(GzcbEntity::getNsrsbh, gzcbEntity.getNsrsbh());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getNsrmc())) {
            queryWrapper.like(GzcbEntity::getNsrmc, gzcbEntity.getNsrmc());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getBz())) {
            queryWrapper.eq(GzcbEntity::getBz, gzcbEntity.getBz());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getJcks())) {
            queryWrapper.eq(GzcbEntity::getJcks, gzcbEntity.getJcks());
        } else {
            queryWrapper.like(GzcbEntity::getJcksdm, substring);
        }
        //应征时间
        if (TsBqUtils.isNotNull(gzcbEntity.getStartTime())) {
            queryWrapper.between(GzcbEntity::getRksj, sdf.parse(gzcbEntity.getStartTime()), sdf.parse(gzcbEntity.getEndTime()));
        }
        queryWrapper.orderByDesc(GzcbEntity::getRksj);
        return page(page, queryWrapper);
    }

    @Override
    public List<GzcbEntity> exportGzcbData(GzcbEntity gzcbEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String substring = currentDeptId.substring(0, 9);
        LambdaQueryWrapper<GzcbEntity> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(gzcbEntity.getAjbh())) {
            queryWrapper.like(GzcbEntity::getAjbh, gzcbEntity.getAjbh());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getNsrsbh())) {
            queryWrapper.like(GzcbEntity::getNsrsbh, gzcbEntity.getNsrsbh());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getNsrmc())) {
            queryWrapper.like(GzcbEntity::getNsrmc, gzcbEntity.getNsrmc());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getBz())) {
            queryWrapper.eq(GzcbEntity::getBz, gzcbEntity.getBz());
        }
        if (TsBqUtils.isNotNull(gzcbEntity.getJcks())) {
            queryWrapper.eq(GzcbEntity::getJcks, gzcbEntity.getJcks());
        } else {
            queryWrapper.like(GzcbEntity::getJcksdm, substring);
        }
        //应征时间
        if (TsBqUtils.isNotNull(gzcbEntity.getStartTime())) {
            queryWrapper.between(GzcbEntity::getRksj, sdf.parse(gzcbEntity.getStartTime()), sdf.parse(gzcbEntity.getEndTime()));
        }
        queryWrapper.orderByDesc(GzcbEntity::getRksj);
        return this.list(queryWrapper);
    }
}